<template>
    <n-form
        ref="formRef"
        label-placement="top"
        :label-width="80"
        :model="form"
        :rules="rules"
    >

        <n-form-item label="请输入名称" path="name">
            <n-input v-model:value="form.name" placeholder="" />
        </n-form-item>
        <n-form-item label="回复类型">
            <jb-radio-group
                v-model:value="form.needCheckSign"
                :value-to-boolean="true"
                url="/api/admin/dictionary/options?typeKey=options_boolean"
            ></jb-radio-group>
        </n-form-item>
    </n-form>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import type { FormInst, FormRules } from 'naive-ui'
import { Rules } from '@/utils'
import { JBoltApi } from '@/utils/request'
import { useResetableData } from '@/hooks/common/use-resetable-data'
import { ResData } from '@/typings/request'

const props = withDefaults(
    defineProps<{
        id: string
    }>(),
    {
        id: ''
    }
)

/** 表单相关 start */
const formRef = ref<FormInst | null>()
interface BucketType {
    id: string
    type: number
    name: string
    briefInfo: string
    needCheckSign: boolean
}
const form = useResetableData<BucketType>({
    id: '',
    type: 1,
    name: '',
    briefInfo: '',
    needCheckSign: false
})
const rules: FormRules = {
    type: new Rules().required('请选择应用类型').value,
    name: new Rules().required('请输入应用名称').value
}
/**
 * 提交表单
 */
async function submit() {
    await formRef.value?.validate()

    let url = props.id ? '/api/admin/app/update' : '/api/admin/app/save'
    await JBoltApi.tryPost(url, form)
    return true
}

function loadEditData() {
    JBoltApi.get<ResData>(`/api/admin/app/${props.id}`).then(({ error, result }) => {
        if (error) return
        form._reset(result.data)
    })
}
const types = ref([])
function loadTypes() {
    JBoltApi.get<ResData>(`/api/admin/app/typesInForm/${props.id}`).then(
        ({ error, result }) => {
            if (error) return
            types.value = result.data
        }
    )
}
onMounted(() => {
    loadTypes()
    if (props.id) {
        loadEditData()
    }

    console.log('8888')
})

defineExpose({
    submit
})

/** 表单相关 end */
</script>
<style scoped></style>
